﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmKhachHang : Form
    {
        public frmKhachHang()
        {
            InitializeComponent();
        }
        int sttkh;
        KhachHangBUS kh = new KhachHangBUS();
        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            sttkh = 0;
            DataTable dt = kh.DataKhachHang();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttkh++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewDSKH.Rows.Add(sttkh, dr["MaKhachHang"].ToString(), dr["TenKhachHang"].ToString(), dr["SoDienThoai"].ToString(), email, dr["DiaChi"].ToString());
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            dataGridViewDSKH.DataSource = kh.DataKhachHang();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhachHangDTO k = new KhachHangDTO();
            k.MaKhachHang = kh.LayMaKhachHang() + 1;
            k.TenKhachHang = txtTenKhachHang.Text.Trim();
            k.SoDienThoai = txtSoDTKhachHang.Text.Trim();
            k.DiaChi = txtDiaChiKhachHang.Text.Trim();
            k.Email = txtEMailKhachHang.Text.Trim();
            kh.insert(k);
            dataGridViewDSKH.Rows.Clear();
            sttkh = 0;
            DataTable dt = kh.DataKhachHang();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttkh++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewDSKH.Rows.Add(sttkh, dr["MaKhachHang"].ToString(), dr["TenKhachHang"].ToString(), dr["SoDienThoai"].ToString(), email, dr["DiaChi"].ToString());
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            kh.Delete(int.Parse(txtMaKhachHang.Text));
            sttkh = 0;
            DataTable dt = kh.DataKhachHang();
            dataGridViewDSKH.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttkh++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewDSKH.Rows.Add(sttkh, dr["MaKhachHang"].ToString(), dr["TenKhachHang"].ToString(), dr["SoDienThoai"].ToString(), email, dr["DiaChi"].ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            KhachHangDTO k = new KhachHangDTO();
            k.MaKhachHang = int.Parse(txtMaKhachHang.Text.Trim());
            k.TenKhachHang = txtTenKhachHang.Text.Trim();
            k.SoDienThoai = txtSoDTKhachHang.Text.Trim();
            k.DiaChi = txtDiaChiKhachHang.Text.Trim();
            kh.update(k);
            dataGridViewDSKH.Rows.Clear();
            sttkh = 0;
            DataTable dt = kh.DataKhachHang();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttkh++;
                string email = "";
                if (dr["Email"] != null)
                    email = dr["Email"].ToString();
                dataGridViewDSKH.Rows.Add(sttkh, dr["MaKhachHang"].ToString(), dr["TenKhachHang"].ToString(), dr["SoDienThoai"].ToString(), email, dr["DiaChi"].ToString());
            }
        }

        private void btnLàmMoi_Click(object sender, EventArgs e)
        {
            txtMaKhachHang.Clear();
            txtTenKhachHang.Clear();
            txtSoDTKhachHang.Clear();
            txtEMailKhachHang.Clear();
            txtDiaChiKhachHang.Clear();
        }



        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }


        private void dataGridViewDSKH_SelectionChanged_1(object sender, EventArgs e)
        {
            int dong = dataGridViewDSKH.SelectedCells.Count;
            if (dong == 0)
                return;
            int bc = dataGridViewDSKH.SelectedCells[0].RowIndex;
            txtMaKhachHang.Text = dataGridViewDSKH.Rows[bc].Cells[1].Value.ToString();
            txtTenKhachHang.Text = dataGridViewDSKH.Rows[bc].Cells[2].Value.ToString();
            txtSoDTKhachHang.Text = dataGridViewDSKH.Rows[bc].Cells[3].Value.ToString();
            txtEMailKhachHang.Text = dataGridViewDSKH.Rows[bc].Cells[4].Value.ToString();
            txtDiaChiKhachHang.Text = dataGridViewDSKH.Rows[bc].Cells[5].Value.ToString();
        }

        private void dataGridViewDSKH_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            //DialogResult dlr = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Warnning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (dlr == DialogResult.Yes)
            //    this.Close();
            Close();
        }
    }
}
